package com.itcam.socket;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        // 127.0.0.1 属于回环地址 表示本机
        // localhost 也表示本机
        try {
            // 创建客户端Socket对象 指定主机地址为本机 端口号为8899
            Socket socket = new Socket("localhost",8899);

            // 根据Socket对象获取到字节输出流
            OutputStream outputStream = socket.getOutputStream();

            // 准备信息
            String message = "你好，服务器";

            // 写入信息
            outputStream.write(message.getBytes());

            // 关闭输出流
            socket.shutdownOutput();

            System.out.println("---------------------------------------------------");

            InputStream inputStream = socket.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            System.out.println("服务器的回话为：" +  reader.readLine());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}